package org.openkoala.security.controller;

import org.apache.shiro.SecurityUtils;
import org.openkoala.koala.commons.InvokeResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Controller
@SuppressWarnings("unused")
public class LogoutController {

	private static final Logger LOGGER = LoggerFactory.getLogger(LogoutController.class);
	
	/**
	 * 用户退出。
	 * 
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public InvokeResult logout() {
        try {
            SecurityUtils.getSubject().logout();
            return InvokeResult.success(getLogoutAddress());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return InvokeResult.success("用户退出失败。");
        }
    }
    
    
    /**
     *  getLogoutAddress.  
     *  @Description：获取退出路径
     *  @Author LiuHongjun
     *  @return 
     *  @Date:2017年11月21日下午1:53:40
     */
    public String getLogoutAddress() {
    	String getLogOutUrl="";
		InputStream propInputStream =LogoutController.class.getResourceAsStream("/drgsys.properties");
		Properties properties = new Properties();
		try {
			properties.load(propInputStream);
			String casUrl = properties.getProperty("cas.sever.url.out");
			String drgUrl = properties.getProperty("drg.sever.url.out");
			String saikuUrl = properties.getProperty("saiku.sever.url.out");
			String isSaikuEnabled = properties.getProperty("isSaikuEnabled");
			if("true".equals(isSaikuEnabled)){
				getLogOutUrl=saikuUrl+"/logout";
            } else {
            	getLogOutUrl=casUrl+"/logout?service="+drgUrl;
            }
		} catch (IOException e) {
			e.printStackTrace();
			LOGGER.error(e.getMessage());
		}finally {
			try {
				propInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	return getLogOutUrl;
    }
}
